<?php

namespace App\Services\GameTool;

use App\Models\Enum\MailStatus;
use App\Models\Enum\MailType;
use App\Repositories\GameTool\GameMailRepository;
use Auth;
use App\Handlers\CsvUploadHandler;

class GameMailService
{
    public function __construct(GameMailRepository $gameMailRepository, CsvUploadHandler $csvUploadHandler)
    {
        $this->gameMailRepository = $gameMailRepository;
        $this->csvUploadHandler = $csvUploadHandler;
    }

    public function create($request)
    {
        $params = $request->all();

        $params['game_id'] = session('game_id');
        $params['creator_admin_id'] = Auth::id();
        $params['approver_admin_id'] = 0;
        $params['status'] = MailStatus::UNCHECK;

        $result = $this->gameMailRepository->create($params);

        return $result;
    }

    public function batchCreate($request)
    {
        $params = $request->all();

        //上传附件
        if ($request->attachment) {
            $result = $this->csvUploadHandler->save($request->attachment, 'mails');
            if ($result) {
                $params['attachment'] = $result['path'];
            }
        }

        $params['game_id'] = session('game_id');
        $params['server_id'] = '';
        $params['nickname'] = '';
        $params['creator_admin_id'] = Auth::id();
        $params['approver_admin_id'] = 0;
        $params['status'] = MailStatus::UNCHECK;
        $params['mail_type'] = MailType::BATCH;

        $result = $this->gameMailRepository->create($params);

        return $result;
    }

    //获取当前用户提交的所有邮件记录
    public function getMails($request)
    {
        $reqPageSize = $request->get('page_size', 10);
        $creator_admin_id = Auth::id();

        $mails = $this->gameMailRepository
            ->where('game_id', session('game_id'))
            ->where('creator_admin_id', $creator_admin_id)
            ->orderBy('id', 'DESC')
            ->paginate($reqPageSize);

        return $mails;
    }

    //获取所有需要审核的邮件
    public function getCheckMails($request)
    {
        $reqPageSize = $request->get('page_size', 10);
        $status = [
            MailStatus::CHECKING,
            MailStatus::AGREE,
            MailStatus::BAN,
        ];

        $mails = $this->gameMailRepository
            ->where('game_id', session('game_id'))
            ->whereIn('status', $status)
            ->orderBy('status', 'ASC')
            ->orderBy('updated_at', 'DESC')
            ->paginate($reqPageSize);

        return $mails;
    }

    //请求游戏邮件API接口
    public function requestGameMailApi($mail)
    {
        $model = 'App\\Games\\' . $mail->game->model_name;

        if (!class_exists($model)) {
            flash('游戏类名不存在')->error()->important();
            return false;
        }

        $class = new $model();

        $result = false;
        if ($mail->mail_type == MailType::BATCH) {
            $result = $class->sendBatchMail($mail);
        } else {
            $result = $class->sendMail($mail);
        }

        return $result;
    }
}